package logic

import (
	"context"

	"go-zero_try/mall/order/rpc/pb/internal/svc"
	"go-zero_try/mall/order/rpc/pb/pb"
	"go-zero_try/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *pb.ModAndRgsReq) (*pb.ModAndRgsReps, error) {
	u1, err := l.svcCtx.UserModel.FindOne(l.ctx, in.UID)
	msg := ""
	if err != nil {
		u2 := model.Users{
			Username: in.Name,
			Password: in.Password,
			Age:      in.Age,
			Uid:      in.UID,
		}
		_, err := l.svcCtx.UserModel.Insert(l.ctx, &u2)
		if err != nil {
			msg = "注册失败"
		} else {
			msg = "注册成功"
		}
	} else {
		if u1.Uid == in.UID {
			msg = "用户ID重复"
		}
	}
	return &pb.ModAndRgsReps{
		Msg:      msg,
		UID:      in.UID,
		Name:     in.Name,
		Password: in.Password,
		Age:      in.Age,
	}, nil
}
